package com.ntl.webcore.tools.dingding.utils;

import com.alibaba.fastjson.JSONObject;
import com.ntl.webcore.tools.dingding.config.DingConfig;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * 钉钉消息发送工具类
 */
public class DingSendUtil {

    /**
     * 发送文本消息
     * @param msg   消息内容
     * @param robotKey  钉钉机器人key
     */
    public static void sendText(String msg, String robotKey){
        try{
            RestTemplate template = new RestTemplate();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("content", msg);
            jsonObject.put("robotKey", robotKey);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);//APPLICATION_FORM_URLENCODED
            // 请求体，包括请求数据 body 和 请求头 headers
            HttpEntity<JSONObject> request = new HttpEntity<>(jsonObject , headers);
            ResponseEntity<Map> map = template.postForEntity(DingConfig.getDingServerUrl(), request , Map.class );
            //ResponseEntity<AjaxResult> response = template.postForEntity(DingConfig.getDingServerUrl(), request , AjaxResult.class );
            //System.out.println(response);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
